//
//  ViewController.swift
//  ExList
//
//  Created by qmsoft_macmini on 16/6/23.
//  Copyright © 2016年 qmsoft_macmini. All rights reserved.
//

import UIKit
import MJRefresh

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, StudyHeadDelegate {

    @IBOutlet weak var tab: UITableView!
    let headID = "StudyHeadID"
    
    var rowAyy:[RowsArrayModel]?
    let qm = QmAPI()
    override func viewDidLoad() {
        super.viewDidLoad()
        tab.delegate = self
        tab.dataSource = self
        let sectionHeaderNib: UINib = UINib(nibName: "StudyHeaderView", bundle: nil)
        self.tab.registerNib(sectionHeaderNib, forHeaderFooterViewReuseIdentifier: headID)
        self.tab.mj_footer = MJRefreshAutoNormalFooter(refreshingTarget: self, refreshingAction: #selector(ViewController.getStudyHeaderData))
        self.tab.mj_header =  MJRefreshNormalHeader(refreshingTarget: self, refreshingAction: #selector(ViewController.getStudyHeaderData))
        qm.getStudyQuery("", k: "", d: "", page: 1, rows: 50, sidx: "create_datetime", sord: "desc") { (info, message, studyMo) in
            if info == 1 {
                self.rowAyy = studyMo?.rows
                self.tab.reloadData()
            }
        }
        
    }
    
    func getStudyHeaderData () {
        if self.tab.mj_header.isRefreshing() {
            self.tab.mj_header.endRefreshing()
        }
        if self.tab.mj_footer.isRefreshing() {
            self.tab.mj_footer.endRefreshing()
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return self.rowAyy?.count ?? 0
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let mo = self.rowAyy?[section] {
            if !mo.isExt {
                return 0
            } else {
                return mo.seriesList?.count ?? 0
            }
        }
        return 0
    }
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 44
    }
    
    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 70
    }

    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let head: StudyHeaderView = tableView.dequeueReusableHeaderFooterViewWithIdentifier(headID) as! StudyHeaderView
        if let mo = self.rowAyy?[section] {
            head.studyHead = self
            head.section = section
            head.txt_name.text = mo.patientname
        }
        return head
    }
    
    func clickHead(section:Int) {
        if let rowMo = self.rowAyy?[section] {
            if rowMo.isExt {
                rowMo.isExt = false
                let heads = NSIndexSet(index: section)
                self.tab.reloadSections(heads, withRowAnimation: UITableViewRowAnimation.Automatic)
            } else {
                if let ayy = self.rowAyy {
                    for i in 0 ..< ayy.count {
                        if ayy[i].isExt {
                            ayy[i].isExt = false
                            print(i)
                            let heads = NSIndexSet(index: i)
                            self.tab.reloadSections(heads, withRowAnimation: UITableViewRowAnimation.Right)
                        }
                    }
                }
                if rowMo.seriesList != nil && rowMo.seriesList?.count > 0 {
                    rowMo.isExt = true
                    let heads = NSIndexSet(index: section)
                    self.tab.reloadSections(heads, withRowAnimation: UITableViewRowAnimation.Automatic)
                } else {
                    qm.getSeriesQuery(rowMo.patientid, studyiuid: rowMo.studyiuid) { (info, message, seriesrray) in
                        rowMo.seriesList = seriesrray
                        rowMo.isExt = true
                        let heads = NSIndexSet(index: section)
                        self.tab.reloadSections(heads, withRowAnimation: UITableViewRowAnimation.Automatic)
                    }
                }
            }
        }
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cellID = "StudyCellID"
        let cell = tableView.dequeueReusableCellWithIdentifier(cellID) as! SeriesTableViewCell
        cell.txt_num.text = self.rowAyy?[indexPath.section].seriesList?[indexPath.row].seriesdesc
        return cell
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        
    }

}

